package com.lepai.component;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 这个类主要是测试Bean的生命周期的过程, 主要是由下面的几个接口来组成的一个Bean的生命周期
 * @see org.springframework.beans.factory.BeanNameAware
 * @see org.springframework.beans.factory.BeanFactoryAware
 * @see org.springframework.context.ApplicationContextAware
 * @see org.springframework.beans.factory.config.BeanPostProcessor
 * @see org.springframework.beans.factory.InitializingBean
 *
 * @author weihaolong
 * @date 2018-12-04 14:46
 * @description
 */
//@Profile("test")
@Conditional(MagicExistsCondition.class)
@Component
@PropertySource("classpath:app.properties")
public class AnnotationInitializationBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanPostProcessor {

    private static final Logger logger = LogManager.getLogger();

    private static String beanName;

    private static BeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    static{
        logger.info("静态初始化方法");
    }

    public AnnotationInitializationBean(){
        logger.info(getClass() + "构造方法执行");
    }

    @Autowired
    private Environment environment;

    @PostConstruct
    public void annotationInitMethod(){
        logger.info("属性占位符：" + environment.getProperty("username"));
        logger.info("属性占位符：" + environment.getProperty("password"));
        logger.info(getClass() + " annotationInitializationBean annotationInitMethod");
    }

    @PreDestroy
    public void annotationPreMethod(){
        logger.info(getClass() + " annotationInitializationBean annotationPreMethod");
    }

    @Override
    public void setBeanName(String name) {
        logger.info(getClass() + "setBeanName方法执行");
        this.beanName = name;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        logger.info(getClass() + "setBeanFactory方法执行");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.info(getClass() + "setApplicationContext方法执行");
        this.applicationContext = applicationContext;
    }

    /**
     * 初始化之前所做的一些操作
     *
     * @param bean 当前的bean的实例
     * @param beanName 当前的bean的名称
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(beanName.equals("lifecycleInitializationBean"))
        logger.info(beanName + " postProcessBeforeInitialization初始化之前执行的方法");
        return null;
    }

    /**
     * 初始化之后所做的一些操作
     * @param bean 当前bean的实例
     * @param beanName 当前bean的名称
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        logger.info(beanName + " postProcessAfterInitialization初始化之后执行的方法");
        return null;
    }
}
